/**
 * @file HandlerBase.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 处理器基类
 * @version 0.0.1
 * @date 2020-07-17
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#include "HandlerBase.hpp"
#include <eXosip2/eX_setup.h>
#include "STL_Log.hpp"

int HandlerBase::sendSimplyResp(const char *uname, struct eXosip_t *excontext, int tid, int status)
{
    osip_message_t *answer = nullptr;

    eXosip_lock(excontext);
    eXosip_message_build_answer(excontext, tid, status, &answer);
    int r = eXosip_message_send_answer(excontext, tid, status, nullptr);
    eXosip_unlock(excontext);

    stl::log("sendSimplyResq: %d to uname:%s, ret: %d", status, uname, r);

    return r;
}

int HandlerBase::sendCallAck(struct eXosip_t *excontext, int did)
{
    eXosip_lock(excontext);

    osip_message_t *ack;
    eXosip_call_build_ack(excontext, did, &ack);
    eXosip_call_send_ack(excontext, did, ack);

    eXosip_unlock(excontext);

    return 0;
}

int HandlerBase::getStatcodeFromResp(osip_message_t *resp)
{
    return resp != nullptr ? resp->status_code : -1;
}

const char *HandlerBase::getMsgIdFromReq(osip_message_t *req)
{
    osip_generic_param_t *tag = nullptr;
    osip_to_get_tag(req->from, &tag);
    if (nullptr == tag || nullptr == tag->gvalue)
    {
        return "";
    }
    return (const char *)tag->gvalue;
}